import config from '@/common/system';

const fileRead = (url) => {
  if (!url) {
    throw new Error('fileRead: url is required');
  }
  return {
    /** 文件预览地址 */
    preview: (path) => {
      if (!path) return '';
      if (/^http/.test(path)) return path;
      return url + path;
    },
    replaceAll: (content, str) => {
      return content.replaceAll(url, str || '{URL}');
    },
    appendAll: (content, str) => {
      return content.replaceAll(str || '{URL}', url);
    },
  };
};

/** 静态文件读取 */
export const publicRead = fileRead(config.pathPublic);
/** 上传文件读取 */
export const uploadRead = fileRead(config.pathUpload);

export default {
  public: publicRead,
  upload: uploadRead,
};
